#include <SDL/SDL.h>
#include <stdio.h>
#include <windows.h>

#include "evis/evis.h"
#include "evis/evis_conf.h"



unsigned char *LoadBitmapFile(char *filename)
{
    FILE *filePtr; //our file pointer
    BITMAPFILEHEADER bitmapFileHeader; //our bitmap file header
    BITMAPINFOHEADER bitmapInfoHeader;
    unsigned char *bitmapImage; //store image data

    //open filename in read binary mode
    filePtr = fopen((uint8_t*) filename,"rb");

    //read the bitmap file header
    fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, filePtr);

    //read the bitmap info header
    fread(&bitmapInfoHeader, sizeof(BITMAPINFOHEADER),1,filePtr);

    //move file point to the begging of bitmap data
    fseek(filePtr, bitmapFileHeader.bfOffBits, 0);

    //allocate enough memory for the bitmap image data
    bitmapImage = (unsigned char*)malloc(bitmapInfoHeader.biSizeImage);

    //read in the bitmap image data
    fread(bitmapImage,1,bitmapInfoHeader.biSizeImage,filePtr);

    //close file and return bitmap iamge data
    fclose(filePtr);
    return bitmapImage;
}

void print_evis_screens(EVIS_SCREEN_T *list)
{
    EVIS_SCREEN_T *current  = list;
    while((current = current->next) != NULL)
        {
        fprintf(stdout,"next element\n");
        fprintf(stdout,"%d\n",current->priority);
        fprintf(stdout,"%d\n",current->onscreen);
        }
}

int main(int argc, char *argv[])
{
     SDL_Surface *buffer;
     EVIS_SCREEN_T *screen;

     uint8_t *fb;
     uint8_t *sprite_option;
     uint8_t *sprite_sun;
     uint8_t *sprite_r;
     uint8_t *sprite_g;
     uint8_t *sprite_b;
     uint8_t *sprite_bg;


     /* Initialize SDL */
     SDL_Init(SDL_INIT_VIDEO);
     /* Initialize the screen / window */
     buffer = SDL_SetVideoMode(WINDOWSIZE_X, WINDOWSIZE_Y, EVIS_COLORDEPTH, SDL_SWSURFACE);
     /* Init Framebuffer to coords 0.0 */
     fb = (Uint8 *)buffer->pixels;


     sprite_bg = LoadBitmapFile("C:/CBDev/embedded Visuals/eVis/background.bmp");
     sprite_option = LoadBitmapFile("C:/CBDev/embedded Visuals/eVis/option.bmp");
     sprite_sun = LoadBitmapFile("C:/CBDev/embedded Visuals/eVis/sun.bmp");
     sprite_r = LoadBitmapFile("C:/CBDev/embedded Visuals/eVis/red.bmp");
     sprite_g = LoadBitmapFile("C:/CBDev/embedded Visuals/eVis/green.bmp");
     sprite_b = LoadBitmapFile("C:/CBDev/embedded Visuals/eVis/blue.bmp");

     EVIS_DRAWSPRITE((uint8_t*) fb,(uint8_t *) sprite_option, 0,50,36,36);
     EVIS_DRAWSPRITE((uint8_t*) fb,(uint8_t *) sprite_sun,    50,50,36,36);
     EVIS_DRAWSPRITE((uint8_t*) fb,(uint8_t *) sprite_r,      100,50,36,36);
     EVIS_DRAWSPRITE((uint8_t*) fb,(uint8_t *) sprite_g,      150,50,36,36);
     EVIS_DRAWSPRITE((uint8_t*) fb,(uint8_t *) sprite_b,      200,50,36,36);

    /****************************************************************************************/
    /****************************************************************************************/
    /******************** EVIS **************************************************************/
    /****************************************************************************************/
    /****************************************************************************************/


    screen = evis_screens_new();

    EVIS_SPRITE_T bg;
    bg.size_x = WINDOWSIZE_X;
    bg.size_y = WINDOWSIZE_Y;
    bg.sprite = sprite_bg;


    EVIS_OBJ_IMG_T background;
    background.uid = 1;
    background.draw = evis_draw_obj_img;
    background.sprite= &bg;

    EVIS_SCREEN_T background_obj;
    background_obj.next = NULL;
    background_obj.last = NULL;
    background_obj.x_pos = 0;
    background_obj.y_pos = 0;
    background_obj.object =  &background;

    screen = evis_screens_add(screen,&background_obj);

    EVIS_SPRITE_T option;
    option.size_x = 36;
    option.size_y = 36;
    option.sprite = sprite_option;

    EVIS_OBJ_IMG_T button_option;
    button_option.uid = 2;
    button_option.draw = evis_draw_obj_img;
    button_option.sprite= &option;

    EVIS_SCREEN_T button_option_obj;
    button_option_obj.next = NULL;
    button_option_obj.last = NULL;
    button_option_obj.x_pos = 100;
    button_option_obj.y_pos = 100;
    button_option_obj.object =  &button_option;

    screen = evis_screens_add(screen,&button_option_obj);

    fprintf(stdout,"Debugging starts here\n");
    evis_render(fb,screen);
    print_evis_screens(screen);
    /* update the screen (aka double buffering) */
    SDL_Flip(buffer);
    SDL_Delay(2000);
    return 1;
}
